package com.jcark.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View

/**
 * Description: 系统状态栏，用于自动填充状态栏位置<br>
 * @author King.Zi<br>
 * @date 2023/8/4<br>
 * Email: 78688689@qq.com
 */
class StatusBarView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) :
    View(context, attrs, defStyleAttr, defStyleRes) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        // 获取宽的测量模式
        // 获取符控件提供的 view 宽的最大值
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), getStatusBarHeight())
    }

    private fun getStatusBarHeight(): Int {
        var statusBarHeight = 0
        try {
            val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return statusBarHeight
    }
}
